package com.hjq.mallproduct.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.hjq.common.validator.group.AddGroup;
import com.hjq.common.validator.group.UpdateGroup;
import com.hjq.common.validator.group.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hjq.mallproduct.entity.BrandEntity;
import com.hjq.mallproduct.service.BrandService;
import com.hjq.common.utils.PageUtils;
import com.hjq.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author Hjq
 * @email 1484397309@qq.com.com
 * @date 2022-05-24 14:18:33
 */
@RestController
@RequestMapping("mallproduct/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;


    @RequestMapping("/infos")
    //@RequiresPermissions("mallproduct:brand:list")
    public R brandInfos(@RequestParam("brandIds")List<Long> brandIds){
        List<BrandEntity> brandEntities = brandService.listByIds(brandIds);

        return R.ok().put("brand", brandEntities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("mallproduct:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("mallproduct:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("mallproduct:brand:save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand){
//            , BindingResult bindingResult){//封装了前面那个参数的校验结果
//        Map<String, String> errorMap = new HashMap<>();
//
//        if(bindingResult.hasErrors()){
//		    //获取错误的字段
//            bindingResult.getFieldErrors().forEach(fieldError -> {
//                String msg =fieldError.getDefaultMessage();
//                String field = fieldError.getField();
//                errorMap.put(field,msg);
//
//            });
//
//		    return R.error(400,"提交数据不合法").put("data",errorMap);
//        }
//        else {
            brandService.save(brand);

            return R.ok();

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("mallproduct:brand:update")
    public R update(@Validated(UpdateGroup.class) @RequestBody BrandEntity brand){
		brandService.updateDetail(brand);

        return R.ok();
    }
    @RequestMapping("/update/status")
    // @RequiresPermissions("mallproduct:brand:update")
    public R updateStatus(@Validated(UpdateStatusGroup.class) @RequestBody BrandEntity brand){
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("mallproduct:brand:delete")
    public R delete(@Valid @RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
